﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.SamplingPlaceForms
{
    public partial class SamplingPlaceDetailForm : DevExpress.XtraEditors.XtraForm
    {
        Action _InitGrid;
        BLL.SamplingPlaceBll samplingPlaceBll = new BLL.SamplingPlaceBll();
        int _operation;
        SamplingPlace _place;
        public SamplingPlaceDetailForm(Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _operation = 1;
            this.Text = "新建取样位置（Create）";
        }

        public SamplingPlaceDetailForm(SamplingPlace place, Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _operation = 2;
            this.textEdit1.Text = place.SamplingPlaceName;
            this.Text = "编辑取样位置（Edit）";
            _place = place;
        }

        private void SamplingPlaceDetailForm_Load(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (_operation == 1)
            {
                string name = this.textEdit1.Text;
                int count = samplingPlaceBll.LoadEntities(m => m.SamplingPlaceName == name).Count();
                if (count > 0)
                {
                    XtraMessageBox.Show(ResourceOverall.DataExists);
                    return;
                }

                SamplingPlace place = new SamplingPlace();
                place.SamplingPlaceID = Guid.NewGuid().ToString();
                place.SamplingPlaceName = this.textEdit1.Text;

                splashScreenManager1.ShowWaitForm();
                var res = samplingPlaceBll.AddSamplingPlaceAndStandard(place);
                splashScreenManager1.CloseWaitForm();

                if (res)
                {
                    XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                    _InitGrid();
                    this.Close();
                }

            }
            else if (_operation == 2)
            {
                string name = this.textEdit1.Text;
                int count = samplingPlaceBll.LoadEntities(m => m.SamplingPlaceName == name).Count();
                if (count > 0)
                {
                    XtraMessageBox.Show(ResourceOverall.DataExists);
                    return;
                }
                using (OJCB4Entities db = new OJCB4Entities())
                {
                    SamplingPlace place=  db.SamplingPlaces.Find(_place.SamplingPlaceID);
                    place.SamplingPlaceName = this.textEdit1.Text;
                    var res = db.SaveChanges()>0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        this.Close();
                    }
                }
            }

        }


    }
}